package com.prs.crm.action.sale;

import java.util.Collection;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import bsh.This;

import com.prs.crm.action.FlowAction;
import com.prs.crm.annotation.Clew;
import com.prs.crm.annotation.FlowActive;
import com.prs.crm.annotation.FlowStart;
import com.prs.crm.domain.crm.Customer;
import com.prs.crm.domain.flow.WorkFlowProcess;
import com.prs.crm.domain.flow.WorkFlowProcessNode;
import com.prs.crm.domain.sale.QuotationDetail;
import com.prs.crm.domain.sale.QuotationSheet;
import com.prs.crm.domain.stock.Product;
import com.prs.crm.domain.stock.ProductStore;
import com.prs.crm.domain.sys.Code;
import com.prs.crm.domain.sys.CodeInvoice;
import com.prs.crm.service.crm.CustomerService;
import com.prs.crm.service.flow.WorkFlowProcessService;
import com.prs.crm.service.sale.QuotationSheetService;
import com.prs.crm.service.stock.ProductService;
import com.prs.crm.service.sys.CodeInvoiceService;
import com.prs.crm.service.sys.CodeService;

public class QuotationSheetAction extends FlowAction<QuotationSheet> {

	private static final long serialVersionUID = 1L;
	
	private String Encode;
	
	private CodeInvoice codeInvoice;
	
	private CodeInvoiceService codeInvoiceService;
	
	private Code code;
	
	private CodeService codeService;
	
	private Collection<CodeInvoice> codelist;

	private Product[] products;

	private String[] pronames;

	private Double[] originalCosts;

	private Double[] nowCosts;

	private Integer[] quotationNums;

	private Double[] rebates;

	private Double[] totalPrices;

	private QuotationSheet quotationsheet;

	private WorkFlowProcess workFlowProcess;

	private QuotationDetail quotationDetail;

	private Collection<QuotationDetail> slist;

	private List<Product> productlist;

	private Customer customer;

	private QuotationSheetService quotationSheetService;

	private WorkFlowProcessService workFlowProcessService;

	private CustomerService customerService;

	private ProductService productService;

	private PaginationSupport pagination;

	private String batchNumber;
	private String batchNum;
	private String listString;
	private String flow;

	@FlowActive(target = "quotationsheet", flowservice = "quotationSheetService")
	@Action("editQuotationSheet")
	public String editQuotationSheet() {
		this.codelist = this.getCodeInvoiceService().getInvoiceCodeType("合同报价");
		return SUCCESS;
	}

	@Action("selectQuoCustomer")
	public String selectQuoCustomer() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Customer.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getCustomerService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	/*
	 * @Action("projectIsNull") public String projectIsNull() { String url =
	 * "editQuotationSheet"; System.out.println("1"); Map map = (Map)
	 * ActionContext.getContext().get("request"); map.put("asdf", url);
	 * System.out.println(url+"        2"); return SUCCESS; }
	 */

	@Action("selectQuoProduct")
	public String selectQuoProduct() {
		//customer=(Customer)this.getCustomerService().getAllCustomer();
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Product.class);
		criteria.add(Restrictions.eq("customer",this.getCustomer()));
		this.setPagination(this.getProductService().listByPage(criteria,
				getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}
	@Action("selectQuoProduct1")
	public String selectQuoProduct1() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				ProductStore.class);
		criteria.add(Restrictions.eq("batchNum", batchNum));

		this.setPagination(this.getProductService().listByPage(criteria,
				getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}

	@Clew(results = { "success", "none" })
	@FlowStart(target = "quotationsheet", flowservice = "quotationSheetService")
	@Action(value = "saveQuotationSheet", results = {
			@Result(name = "success", type = "redirect", location = "listQuotationSheet"),
			@Result(name = "error", type = "chain", location = "editQuotationSheet"),
			@Result(name = "none", type = "redirect", location = "listQuotationSheet") })
	public String saveQuotationSheet() {
		if ("n".equals(Encode)) {
			int EncodeRplice = this.getCodeService().getBackRpliceAll("手动编码","销售报价单",
					quotationsheet.getQuotationCode());
			if (EncodeRplice != 0) {
				this.addActionError("编码重复!请重新输入");
				return ERROR;
			}
		}
		if("x".equals(Encode)||"".equals(Encode)){
			this.addActionError("请选择编码类型");		
			return ERROR;
		}
		
		if(flow==null||flow.equals("")){
			this.addActionError("请添加审核流程");
			return ERROR;
		}
		if(quotationsheet.getQuotationName() == null ||quotationsheet.getQuotationName().equals("")) {
			this.addActionError("请添加报价主题");
			return ERROR;
		}
		if(quotationsheet.getQuotationCode() == null ||quotationsheet.getQuotationCode().equals("")) {
			this.addActionError("请添加报价编号");
			return ERROR;
		}
		if(quotationsheet.getCustomer() == null) {
			this.addActionError("请添加关联客户");
			return ERROR;
		}
		if(products == null || products.length == 0) {
			this.addActionError("商品不能为空");
			return ERROR;
		}
		
		if ("n".equals(Encode)) {
			Code codes = new Code();
			codes.setCodeValue(quotationsheet.getQuotationCode());
		    codes.setCodeMolde("销售报价单");
		    codes.setEntityName(this.getClass().getName());
			codes.setCodeType("手动编码");
			this.getCodeService().save(codes);
			quotationsheet.setCode(codes);
			this.getQuotationSheetService().saveQuotation(quotationsheet, products,
					pronames, originalCosts, nowCosts, quotationNums, rebates,
					totalPrices);
			
		} else {
			CodeInvoice Vob = this.getCodeInvoiceService().getVobject(
					codeInvoice.getId());
			Code codes = new Code();
			codes.setCodeValue(quotationsheet.getQuotationCode());
			codes.setCodeMolde("销售报价单");
		    codes.setEntityName(this.getClass().getName());
			Vob.setNumBackUp(Increase(Vob.getNumBackUp()));
			this.getCodeInvoiceService().saveOrUpdate(Vob);
			codes.setCodeType(Vob.getCodePlan());
			this.getCodeService().save(codes);
			quotationsheet.setCode(codes);
			this.getQuotationSheetService().saveQuotation(quotationsheet, products,
					pronames, originalCosts, nowCosts, quotationNums, rebates,
					totalPrices);
		}
		
		

		/**
		 * 编码产生
		 */
		


		
		this.setPrompt("操作成功");
		return SUCCESS;

	}

	/**
	 * 修改报价单产品单价
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@Action(value = "updaQuotationSheet", results = {
			@Result(name = "success", type = "redirect", location = "listQuotationSheetProcess"),
			@Result(name = "none", type = "redirect", location = "listQuotationSheetProcess") })
	public String updaQuotationSheet() {
		for (int i = 0; i < products.length; i++) {
			QuotationDetail quotationDetailObject = this
					.getQuotationSheetService().getQuotationDetail(products[i],
							quotationsheet);
			quotationDetailObject.setNowCost(nowCosts[i]);
			quotationDetailObject.setQuotationNum(quotationNums[i]);
			quotationDetailObject.setRebate(rebates[i]);
			quotationDetailObject.setTotalPrice(totalPrices[i]);
			this.getQuotationSheetService().saveOrUpdate(quotationDetailObject);
		}
		QuotationSheet quotationSheetObject = this.getQuotationSheetService()
				.getQuotationSheet(quotationsheet.getId());
		quotationSheetObject.setFlowstatus(1);
		this.getQuotationSheetService().saveOrUpdateQuotationSheet(
				quotationSheetObject);
		workFlowProcess = quotationSheetObject.getProcess();
		WorkFlowProcess workFlowProcessObject = this
				.getWorkflowProcessService().getWorkFlowProcess(
						workFlowProcess.getId());
		workFlowProcessObject.setResult(0);
		workFlowProcessObject.setStatus(1);
		this.getWorkflowProcessService().saveOrUpdate(workFlowProcessObject);
		List<WorkFlowProcessNode> workFlowProcessNodeObjectList = this
				.getWorkflowProcessService().getWorkFlowProcessNode(
						workFlowProcessObject);
		for (int m = 0; m < workFlowProcessNodeObjectList.size(); m++) {
			WorkFlowProcessNode workFlowProcessNodeObject = workFlowProcessNodeObjectList
					.get(m);
			if (m == 0) {
				workFlowProcessNodeObject.setResult(0);
				workFlowProcessNodeObject.setStatus(1);
				this.getWorkflowProcessService()
						.saveOrUpdateWorkFlowProcessNode(
								workFlowProcessNodeObject);
			} else {
				workFlowProcessNodeObject.setResult(0);
				workFlowProcessNodeObject.setStatus(0);
				this.getWorkflowProcessService()
						.saveOrUpdateWorkFlowProcessNode(
								workFlowProcessNodeObject);
			}

		}
		this.setPrompt("操作成功");
		return SUCCESS;
	}

	@Action("listQuotationSheet")
	public String excute() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				QuotationSheet.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getQuotationSheetService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("listQuotationSheetProcess")
	public String excute1() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				QuotationSheet.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		criteria.add(Restrictions.eq("flowstatus", 3));
		this.setPagination(this.getQuotationSheetService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("viewQuotationSheet")
	public String viewQuotationSheet() {
		quotationsheet = this.getQuotationSheetService().get(
				quotationsheet.getId());
		this.slist = quotationsheet.getQuodetails();
		this.displayEndNodes(quotationsheet);
		return SUCCESS;
	}

	@Action("upPriceAdjustmentQuotationSheet")
	public String upPriceAdjustmentQuotationSheet() {
		quotationsheet = this.getQuotationSheetService().get(
				quotationsheet.getId());
		this.slist = quotationsheet.getQuodetails();
		this.displayEndNodes(quotationsheet);
		return SUCCESS;
	}
	public static String Increase(String backupNum) {
		String Inc = "";
		int lengths = backupNum.length();
		String lengthss = (Integer.parseInt(backupNum) + 1) + "";
		int increase = (lengthss.trim()).length();
		if (lengths != increase) {
			String In = "";
			for (int i = 0; i < lengths - increase; i++) {
				In = In + "0";
			}
			In = (In + lengthss).trim();
			Inc = In;
		} else {
			Inc = lengthss;
		}
		return Inc.trim();
	}
	
	public QuotationSheet getQuotationsheet() {
		return quotationsheet;
	}

	public void setQuotationsheet(QuotationSheet quotationsheet) {
		this.quotationsheet = quotationsheet;
	}

	public QuotationSheetService getQuotationSheetService() {
		return quotationSheetService;
	}

	public void setQuotationSheetService(
			QuotationSheetService quotationSheetService) {
		this.quotationSheetService = quotationSheetService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public CustomerService getCustomerService() {
		return customerService;
	}

	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public ProductService getProductService() {
		return productService;
	}

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	public Product[] getProducts() {
		return products;
	}

	public void setProducts(Product[] products) {
		this.products = products;
	}

	public String[] getPronames() {
		return pronames;
	}

	public void setPronames(String[] pronames) {
		this.pronames = pronames;
	}

	public Double[] getOriginalCosts() {
		return originalCosts;
	}

	public void setOriginalCosts(Double[] originalCosts) {
		this.originalCosts = originalCosts;
	}

	public Double[] getNowCosts() {
		return nowCosts;
	}

	public void setNowCosts(Double[] nowCosts) {
		this.nowCosts = nowCosts;
	}

	public Integer[] getQuotationNums() {
		return quotationNums;
	}

	public void setQuotationNums(Integer[] quotationNums) {
		this.quotationNums = quotationNums;
	}

	public Double[] getRebates() {
		return rebates;
	}

	public void setRebates(Double[] rebates) {
		this.rebates = rebates;
	}

	public Double[] getTotalPrices() {
		return totalPrices;
	}

	public void setTotalPrices(Double[] totalPrices) {
		this.totalPrices = totalPrices;
	}

	public Collection<QuotationDetail> getSlist() {
		return slist;
	}

	public void setSlist(Collection<QuotationDetail> slist) {
		this.slist = slist;
	}

	public QuotationDetail getQuotationDetail() {
		return quotationDetail;
	}

	public void setQuotationDetail(QuotationDetail quotationDetail) {
		this.quotationDetail = quotationDetail;
	}

	public WorkFlowProcess getWorkFlowProcess() {
		return workFlowProcess;
	}

	public void setWorkFlowProcess(WorkFlowProcess workFlowProcess) {
		this.workFlowProcess = workFlowProcess;
	}

	public WorkFlowProcessService getWorkFlowProcessService() {
		return workFlowProcessService;
	}

	public void setWorkFlowProcessService(
			WorkFlowProcessService workFlowProcessService) {
		this.workFlowProcessService = workFlowProcessService;
	}

	public String getBatchNumber() {
		return batchNumber;
	}

	public void setBatchNumber(String batchNumber) {
		this.batchNumber = batchNumber;
	}

	public List<Product> getProductlist() {
		return productlist;
	}

	public void setProductlist(List<Product> productlist) {
		this.productlist = productlist;
	}

	public String getBatchNum() {
		return batchNum;
	}

	public void setBatchNum(String batchNum) {
		this.batchNum = batchNum;
	}

	public String getListString() {
		return listString;
	}

	public void setListString(String listString) {
		this.listString = listString;
	}

	public String getFlow() {
		return flow;
	}

	public void setFlow(String flow) {
		this.flow = flow;
	}

	public String getEncode() {
		return Encode;
	}

	public void setEncode(String encode) {
		Encode = encode;
	}

	public CodeInvoice getCodeInvoice() {
		return codeInvoice;
	}

	public void setCodeInvoice(CodeInvoice codeInvoice) {
		this.codeInvoice = codeInvoice;
	}

	public CodeInvoiceService getCodeInvoiceService() {
		return codeInvoiceService;
	}

	public void setCodeInvoiceService(CodeInvoiceService codeInvoiceService) {
		this.codeInvoiceService = codeInvoiceService;
	}

	public Code getCode() {
		return code;
	}

	public void setCode(Code code) {
		this.code = code;
	}

	public CodeService getCodeService() {
		return codeService;
	}

	public void setCodeService(CodeService codeService) {
		this.codeService = codeService;
	}

	public Collection<CodeInvoice> getCodelist() {
		return codelist;
	}

	public void setCodelist(Collection<CodeInvoice> codelist) {
		this.codelist = codelist;
	}

}
